//
//  SongerViewController.m
//  NewMusic
//
//  Created by chuxinyu123 on 15/11/17.
//  Copyright © 2015年 Going. All rights reserved.
//

#import "SongerViewController.h"
#import "CustomerNetworking.h"
#import <UIImageView+WebCache.h>
#import <SDWebImageManager.h>
#import "SongerDataTableViewCell.h"

#import "SongerDataViewController.h"

@interface SongerViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *array;

@end

@implementation SongerViewController

- (void)dealloc
{
    [_tableView release];
    [_array release];
    [_songerModel release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIImageView *backImage = [[UIImageView alloc] initWithFrame:self.view.frame];
    backImage.image = [UIImage imageNamed:@"backGroud.jpg"];
    [self.view addSubview:backImage];
    
    
    self.array = [NSMutableArray array];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 667 - 67) style:UITableViewStylePlain];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    UIView *footView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.tableFooterView = footView;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView registerClass:[SongerDataTableViewCell class] forCellReuseIdentifier:@"songerDataTableViewCell"];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 50, 40);
    [button setImage:[UIImage imageNamed:@"icon_fanhui"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(popAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    
    [self.view addSubview:_tableView];
    [_tableView release];
    [backImage release];
    [footView release];
}

- (void)popAction:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setSongerModel:(SongerModel *)songerModel
{
    if (_songerModel != songerModel) {
        [_songerModel release];
        _songerModel = [songerModel retain];
    }
    self.navigationItem.title = songerModel.title;
    [self customer];
}
- (void)customer
{
    NSString *string = [NSString stringWithFormat:@"http://v1.ard.tj.itlily.com/ttpod?a=getnewttpod&id=%@&size=1000&page=1&app=ttpod&v=v8.1.1.2015110616&user_id=0&mid=iPhone5C&f=f234&s=s310&imsi=&hid=&splus=8.4.1&active=1&net=2&openudid=2b2eba0e983843cf5ebcfa76b63e1682221e74ef&idfa=C142307D-73E9-409F-88B5-2F7B28503941&utdid=VkKgUwkPrlsDAINyD9AzAIoe&alf=700606&bundle_id=com.ttpod.music&latitude=38.8825&longtitude=121.5399", _songerModel.ID];
    [CustomerNetworking customerConnectWithURL:string andParameters:nil andBlock:^(id response) {
        NSDictionary *dictionary = response;
        for (NSDictionary *dic in [dictionary objectForKey:@"data"]) {
            SongerDataModel *songerDataModel = [SongerDataModel songerDataModelWithDictionary:dic];
            [self.array addObject:songerDataModel];
        }
        [self.tableView reloadData];
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SongerDataTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"songerDataTableViewCell" forIndexPath:indexPath];
    cell.songerDataModel = [_array objectAtIndex:indexPath.row];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SongerDataViewController *songer = [[SongerDataViewController alloc] init];
    songer.singerName = [[_array objectAtIndex:indexPath.row] singer_name];
    songer.pic_Url = [[_array objectAtIndex:indexPath.row] pic_url];
    songer.IDString = [[_array objectAtIndex:indexPath.row] singer_id];
    [self.navigationController pushViewController:songer animated:YES];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 96;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
